package com.example.yunJie.digitalSentry.control;

import com.example.yunJie.digitalSentry.entity.MarketEntity;
import com.example.yunJie.digitalSentry.entity.MarketPostEntity;
import com.example.yunJie.digitalSentry.entity.PostEntity;
import com.example.yunJie.digitalSentry.service.MarketPostService;
import com.example.yunJie.digitalSentry.service.MarketService;
import com.example.yunJie.digitalSentry.service.PostService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/MarketPost")
public class MarketPostControl {
    @Autowired
    private PostService postService;
    @Autowired
    private MarketPostService marketPostService;
    @Autowired
    private MarketService marketService;


    @RequestMapping("/add")
    public void insert(MarketPostEntity marketPostEntity){
        PostEntity idByPostName1 = postService.findIdByPostName(marketPostEntity.getPostName());
        if (idByPostName1 == null){
            PostEntity postEntity = new PostEntity();
            postEntity.setPostName(marketPostEntity.getPostName());
            postService.insert(postEntity);
        }

        PostEntity idByPostName = postService.findIdByPostName(marketPostEntity.getPostName());
        MarketEntity idByMarketName = marketService.findIdByMarketName(marketPostEntity.getMarketName());
        marketPostEntity.setMarketId(String.valueOf(idByMarketName.getId()));
        marketPostEntity.setPostId(String.valueOf(idByPostName.getId()));
        if (marketPostService.find(String.valueOf(idByMarketName.getId()),String.valueOf(idByPostName.getId())) == null){
            marketPostService.insert(marketPostEntity);
        }

    }

    @RequestMapping("/update")
    public int update(MarketPostEntity marketPostEntity){
        PostEntity idByPostName1 = postService.findIdByPostName(marketPostEntity.getPostName());
        if (idByPostName1 == null){
            PostEntity postEntity = new PostEntity();
            postEntity.setPostName(marketPostEntity.getPostName());
            postService.insert(postEntity);
        }
        PostEntity idByPostName = postService.findIdByPostName(marketPostEntity.getPostName());
        MarketEntity idByMarketName = marketService.findIdByMarketName(marketPostEntity.getMarketName());
        marketPostEntity.setMarketId(String.valueOf(idByMarketName.getId()));
        marketPostEntity.setPostId(String.valueOf(idByPostName.getId()));
        if (marketPostService.find(String.valueOf(idByMarketName.getId()),String.valueOf(idByPostName.getId())) == null){
           return marketPostService.update(marketPostEntity);
        }
//        return   marketPostService.update(marketPostEntity);
        return 0;
    }

    @RequestMapping("/deleteById")
    public int  deleteById(String id){
        return marketPostService.deleteById(id);
    }

    @RequestMapping("/deleteIdList")
    public int  deleteIdList(List<Integer> id){
        return marketPostService.deleteIdList(id);
    }

    @RequestMapping("/findId")
    public MarketPostEntity  findId(String id){
        return marketPostService.findId(id);
    }

    @RequestMapping("/findAll")
    public List<MarketPostEntity>  findAll(MarketPostEntity marketPostEntity){
        List<MarketPostEntity> all = marketPostService.findAll(marketPostEntity);
        return all;
    }
}
